package com.study.qqclient.service;

import com.study.qqcommon.Message;
import com.study.qqcommon.MessageType;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ClientConnectServerThread extends Thread {
    private Socket socket;
    private ObjectInputStream ois;
    //输出流
    private ObjectOutputStream oos;

    public ClientConnectServerThread(Socket socket) {
        this.socket = socket;
        try {
            this.ois = new ObjectInputStream(socket.getInputStream());
            this.oos = new ObjectOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }

    @Override
    public void run() {
        while (true) {
            try {
                    Message message = (Message) ois.readObject();
                    System.out.println("客户端收到消息：" + message);
                    switch (message.getMesType()) {
                        case MessageType.MESSAGE_RET_ONLINE_FRIEND:
                            String[] users = message.getContent().split(" ");
                            System.out.println("Online users list:");
                            for (String user : users) {
                                System.out.println("User: " + user);
                            }
                            break;
                        case MessageType.MESSAGE_CLIENT_EXIT:
                            socket.close();
                            System.out.println(message.getSender() + "退出");
                            return;
                        case MessageType.MESSAGE_COMM_MES:
                            System.out.println(message.getSender() + "给" + message.getGetter() + "发送消息：" + message.getContent());
                            break;
                        case MessageType.MESSAGE_TO_ALL_MES:
                            System.out.println(message.getSender() + "给所有人发送消息：" + message.getContent());
                            break;
                        case MessageType.MESSAGE_FILE_MES:
                            try (FileOutputStream fos = new FileOutputStream(message.getDest())) {
                                fos.write(message.getFileBytes());
                            }
                            System.out.println("接收到文件消息" + message.getSender() + "给" + message.getGetter() + "发送的文件：" + message.getSrc());
                            System.out.println("文件保存在：" + message.getDest());
                            break;
                        default:
                            System.out.println("服务端收到客户端发来的其他消息类型：" + message.getMesType());
                            break;
                }
            } catch (IOException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }
}